﻿from configparser import RawConfigParser
import sys


#rewrite
class ReaperConfigParser(RawConfigParser):

    def optionxform(self, optionstr):
        return optionstr

    def _convert_to_boolean(self, value):
        """Return a boolean value translating from other types if necessary.
        """
        if value not in self.BOOLEAN_STATES:
            raise ValueError('Not a boolean: %s' % value)
        return self.BOOLEAN_STATES[value]


def combine_translate_with_template(ReaperLangPackFile='zh_CN.reaperLangPack',tpl= ReaperConfigParser(),trans= ReaperConfigParser(), header='#NAME:简体中文(Simple Chinese)\n;Author:wondro\n\n'): #return string


    rtn = header

    for section in tpl._sections:
        
        rtn+='\n[%s]\n' % section
        for op in tpl._sections[section]:

            rtn+=';%s\n' % tpl._sections[section][op] 
            
            if trans.has_option(section,op[1:]):
                rtn+='%s=%s\n\n' % ( op[1:],trans._sections[section][op[1:]] )
            else:
                rtn+='%s=%s\n\n' % ( op[1:],tpl._sections[section][op] )

    with open( ReaperLangPackFile,'w',encoding='UTF-8-SIG') as f:
        f.write(rtn)





try:

    tplMCP=ReaperConfigParser(delimiters=('='), comment_prefixes=('#',';^','; '))
    transMCP = ReaperConfigParser(delimiters=('='), comment_prefixes=('#',';'))


    tplfile=sys.argv[1]
    transfile=sys.argv[2]

    tplMCP.read(tplfile,encoding='UTF-8-SIG')
    transMCP.read(transfile,encoding='UTF-8-SIG' )

    combine_translate_with_template(tpl=tplMCP,trans=transMCP)
    print('done!')
except Exception as e:
    print('''
Usage:
    python  %script_name%  %templete_file_name%  %translate_file_name%
    Notice: you need python3 to run this script.
            template and translation shoud be encoded with UTF-8-SIG,you can use notepad.exe to save file to UTF-8,
            to update template,just download the new template from reaper.fm and replace the old one,
            you can copy and rename the release file as your new translate.ini to update your translation file.
''')
